package cn.xiaye.httpserver.bean;

import java.sql.*;
import java.util.*;

public class Person {
	private int id_p;
	private String lastname;
	private String firstname;
	private String address;
	private String city;

	public int getId_p() {
		return id_p;
	}

	public void setId_p(int idP) {
		id_p = idP;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getAdress() {
		return address;
	}

	public void setAdress(String adress) {
		this.address = adress;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public static List<Person> queryAll() {
		String sql = "select * from persons";
		List<Person> list = new ArrayList<Person>();
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydata", "root", "root");
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			while (rs.next()) {
				Person p = new Person();
				p.setId_p(rs.getInt("id_p"));
				p.setLastname(rs.getString("lastname"));
				p.setFirstname(rs.getString("firstname"));
				p.setAdress(rs.getString("adress"));
				p.setCity(rs.getString("city"));
				list.add(p);
			}
			rs.close();
			st.close();
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public String toString() {
		return "id:" + id_p + "  lastname:" + lastname + "  firstname:" + firstname + "   address:" + address + "   city:" + city;
	}

}
